import 'package:motion_toast/motion_toast.dart';
import 'package:motion_toast/resources/arrays.dart';
import 'package:nylo_framework/nylo_framework.dart';

import '../../config/validation_rules.dart';
import '../../resources/pages/home_page.dart';
import '../../resources/pages/login/phonecode_page.dart';
import 'controller.dart';
import 'package:flutter/widgets.dart';

class LoginController extends Controller {
  bool isChecked = false;
  bool successLoading = false;
  final FocusNode phoneFocusNode = FocusNode();
  final FocusNode phoneCodeFocusNode = FocusNode();
  TextEditingController phoneController = TextEditingController();
  TextEditingController phoneCodeController = TextEditingController();

  construct(BuildContext context) {
    super.construct(context);
  }

  /// 获取验证码校验
  void check(context) {
    debugPrint(phoneController.text);
    if (phoneController.text.isEmpty) {
      MotionToast.warning(
        description: Text("请填写手机号"),
        position: MotionToastPosition.top,
        animationType: AnimationType.fromTop,
        animationCurve: Curves.fastEaseInToSlowEaseOut,
      ).show(context);
      return;
    }
    if (!PhoneValidation(phoneController.text)) {
      MotionToast.warning(
        description: Text("手机号格式错误"),
        position: MotionToastPosition.top,
        animationType: AnimationType.fromTop,
        animationCurve: Curves.fastEaseInToSlowEaseOut,
      ).show(context);
      return;
    }
    if (!isChecked) {
      MotionToast.warning(
        description: Text("请阅读并同意《用户协议》和《隐私协议》"),
        position: MotionToastPosition.top,
        animationType: AnimationType.fromTop,
        animationCurve: Curves.fastEaseInToSlowEaseOut,
      ).show(context);
      return;
    }

    Navigator.pushNamed(context, PhonecodePage.path);
  }

  /// 手机号格式处理
  String handlePhone(String phone) {
    if (phone.length == 11) {
      return '${phone.substring(0, 3)}****${phone.substring(7, 11)}';
    } else {
      return phone;
    }
  }

  /// 验证码登录
  Future<bool> login(BuildContext context,refreshPage) async {
    NyLogger.debug("验证码" + phoneCodeController.text);
    if (phoneCodeController.text.isEmpty) {
      MotionToast.warning(
        description: Text("请填写验证码"),
        position: MotionToastPosition.top,
        animationType: AnimationType.fromTop,
        animationCurve: Curves.fastEaseInToSlowEaseOut,
      ).show(context);
      NyLogger.debug("验证码为空");
      return false;
    }

    if ("1111"      != phoneCodeController.text) {
      MotionToast.warning(
        description: Text("验证码不正确或已过期"),
        position: MotionToastPosition.top,
        animationType: AnimationType.fromTop,
        animationCurve: Curves.fastEaseInToSlowEaseOut,
      ).show(context);
      return false;
    }
    // 登录api
    // 登录成功
    NyLogger.debug("登录成功");


     successLoading = true;
    refreshPage();
    /// 做登录成功之后的事
    await Future.delayed(Duration(seconds: 3));
     successLoading = false;
    refreshPage();
    Navigator.pushNamedAndRemoveUntil(context, HomePage.path, (route) => false);

    return true;

  }
}
